package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomReservationConfigMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomReservationConfig;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.reservation.RoomReservationConfigPageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class RoomReservationRepository extends ServiceImpl<RoomReservationConfigMapper, RoomReservationConfig> {

    public IPage<RoomReservationConfigPageDTO> page(Integer pageNo, Integer pageSize, Long apartmentId, String apartmentName) {
        Page<RoomReservationConfig> page = Page.of(pageNo, pageSize);
        QueryWrapper<RoomReservationConfig> wrapper = Wrappers.query();
        wrapper.eq("ra.deleted", false)
                .eq("rrc.deleted", false)
                .eq(Objects.nonNull(apartmentId), "rrc.apartment_id", apartmentId)
                .like(StrUtil.isNotEmpty(apartmentName), "ra.name", apartmentName);
        IPage<RoomReservationConfigPageDTO> iPage = this.getBaseMapper().pageList(wrapper, page);
        return iPage;
    }

    public RoomReservationConfig getByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<RoomReservationConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomReservationConfig::getApartmentId,apartmentId);
        return baseMapper.selectOne(wrapper);
    }
}
